﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpBolt.Graphics.GraphicInstances;
using SharpBolt.Graphics.GraphicSources;
using SharpBolt.Basics;
using SharpBolt.Graphics;
using InductBase;

namespace SharpBolt.Visual
{
    class VisualImageInstance
    {
        GraphicInstance graphicInstance;
        public GraphicInstance GraphicInstance
        {
            get { return graphicInstance; }
            set { graphicInstance = value; }
        }

        public VisualImageInstance(GraphicInstance graphicInstance)
        {
            this.graphicInstance = graphicInstance;
            Engine.InputObserver.Pointer.Move += new EventHandler(Pointer_Move);
        }

        void Pointer_Move(object sender, EventArgs e)
        {
            SbManipulator pointer = (SbManipulator)sender;
            bool res = HitTest(pointer.InViewPosition());
            if (IsHovered && !res)
            {
                if (PointerLeave != null) PointerLeave(this, EventArgs.Empty);
            }

            if (!IsHovered && res)
            {
                if (PointerEnter != null) PointerEnter(this, EventArgs.Empty);
            }
        }

        public event EventHandler PointerEnter;

        public event EventHandler PointerLeave;

        bool isHovered;
        public bool IsHovered
        {
            get { return isHovered;}
        }

        public bool IsInView
        {
            get {
                return Collision.Collider.ParallelRectanglesIntersect(
                    Rec.FromLocationSize(
                    graphicInstance.Transformation.Position,
                    graphicInstance.Size * graphicInstance.Transformation.Scale),
                    Engine.GraphicRender.View);
                }
        }

        public bool HitTest(InductBase.Vec2 point)
        {
            return Collision.Collider.RectanglesContains(
                point,
                Rec.FromLocationSize(Vec2.Null, graphicInstance.Size),
                graphicInstance.Transformation);
        }
    }
}
